﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Xml;
using System.IO;

namespace POC.Configuration
{
    public class AppSetting
    {
        public bool IsLocalAccount { get; private set; }
        public bool IsAsyncOperation { get; private set; }
        public bool IsHtttps { get; set; }
        public string AccountName { get; private set; }
        public string AccountKey { get; set; }
        public string AccountSecondaryKey { get; set; }
        public bool UseSystemProxy { get; set; }

        public string BaseUri { get; set; }
        public string HostedDomain { get; set; }
        public string ConfigFile { get; set; }
        public string NodeName { get; set; }

        public void Requery()
        {
            this.IsLocalAccount = (ApplicationConfiguration.Get(ConfigKeyConstant.UseLocalAccount).ToLower() == "true");
            this.IsAsyncOperation = (ApplicationConfiguration.Get(ConfigKeyConstant.Async).ToLower() == "true");
            this.IsHtttps = ApplicationConfiguration.Get(ConfigKeyConstant.IsHttps).ToBoolean();
            this.AccountName = this.IsLocalAccount ?
                ApplicationConfiguration.Get(ConfigKeyConstant.DevelopmentAzureAccount) :
                 ApplicationConfiguration.Get(ConfigKeyConstant.AzureAccount);
            this.AccountKey = ApplicationConfiguration.Get(ConfigKeyConstant.StorageKey);
            this.AccountSecondaryKey = ApplicationConfiguration.Get(ConfigKeyConstant.SecondaryStorageKey);
            this.BaseUri = ApplicationConfiguration.Get(ConfigKeyConstant.BaseUri);
            this.HostedDomain = ApplicationConfiguration.Get(ConfigKeyConstant.Host);
            this.ConfigFile = ApplicationConfiguration.Get(ConfigKeyConstant.ConfigFile);
            this.NodeName = ApplicationConfiguration.Get(ConfigKeyConstant.NodeName);
            this.UseSystemProxy = ApplicationConfiguration.Get(ConfigKeyConstant.UseSystemProxy).ToBoolean();
        }
        public string this[string key]
        {
            get
            {
                string val = ApplicationConfiguration.Get(key);
                return val;
            }
        }
        public IDictionary<string, string> GetSettings()
        {
            IDictionary<string, string> nodes = ApplicationConfiguration.List();
            Dictionary<string, string> sets = new Dictionary<string, string>()
            {
                { "IsLocalAccount", IsLocalAccount.ToString()} ,
                { "IsAsyncOperation" , IsAsyncOperation.ToString()},
                {"IsHtttps" ,IsHtttps.ToString()},
                {"AccountName" , AccountName},               
                {"AccountKey" ,AccountKey},
                {"AccountSecondaryKey" , AccountSecondaryKey},
                {"BaseUri" ,BaseUri},
                {"HostedDomain" , HostedDomain},
                {"ConfigFile" , ConfigFile},
                {"NodeName" , NodeName},
                {"UseSystemProxy" ,UseSystemProxy.ToString()}
            };
            var comparer = new KeyEqualityComparer<string, string>();
            nodes = sets.Union(nodes, comparer).ToDictionary(d => d.Key, k => k.Value);
            return nodes;
        }
        public void ReInit(Action initAction)
        {
            Requery();
            initAction.Invoke();
        }
        static AppSetting _instance = null;
        public static AppSetting Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AppSetting();
                    _instance.Requery();
                }
                return _instance;
            }
        }
        public static Uri BuildUri(string accountName, string storageType, bool isSecure = false, bool isLocal = false)
        {
            Uri uri = null;
            if (isLocal)
            {
                string absoluteUri = string.Format(CultureInfo.InvariantCulture, "{0}://{1}/{2}", isSecure ? "https" : "http", storageType, accountName);
                uri = new Uri(absoluteUri);
            }
            else
            {
                string absoluteUri = string.Format(CultureInfo.InvariantCulture, "{0}://{1}.{2}", isSecure ? "https" : "http", accountName, storageType);
                uri = new Uri(absoluteUri);
            }
            return uri;
        }


    }
    public class KeyEqualityComparer<T, U> : IEqualityComparer<KeyValuePair<T, U>>
    {
        public bool Equals(KeyValuePair<T, U> x, KeyValuePair<T, U> y)
        {
            return x.Key.Equals(y.Key);
        }

        public int GetHashCode(KeyValuePair<T, U> obj)
        {
            return obj.Key.GetHashCode();
        }
    }
}
